﻿using System;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using BikeBouncer.BusinessLayer.Enums;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer.Entities
{
	[DataContract, Serializable]
	public class Note : BaseEntity
	{
		#region Properties

		[DataMember]
		public Item Item { get; set; }

		[DataMember]
		public User User { get; set; }

		[DataMember]
		public Address Address { get; set; }

		[DataMember]
		public String Details { get; set; }

		[DataMember]
		public DateTime ReferenceDate { get; set; }

		[DataMember]
		public DateTime PostedDate { get; set; }

		#endregion

		#region Internal use only properties

		public String IP { get; set; }

		#endregion

		#region Constructor

		public Note()
		{
			Item = new Item();
			User = new User();
			Address = new Address();
		}

		public Note(Int32 id)
		{
			Construct(DataAccess.Note_Get(id));
		}

		public Note(SqlDataReader reader)
		{
			Construct(reader);
		}

		#endregion

		#region Loading

		public override void Load(SqlDataReader reader)
		{
			Id = DataHandler.GetInt32(reader, "NoteId");
			IP = DataHandler.GetString(reader, "NoteIP");
			Details = DataHandler.GetString(reader, "NoteDetails");
			ReferenceDate = DataHandler.GetDateTime(reader, "NoteReferenceDate");
			PostedDate = DataHandler.GetDateTime(reader, "NotePostedDate");
			Active = DataHandler.GetBoolean(reader, "NoteActive");

			Item = new Item();
			Item.Id = DataHandler.GetInt32(reader, "ItemId");
			Item.Title = DataHandler.GetString(reader, "ItemTitle");

			if (!String.IsNullOrEmpty(Item.Title))
			{
				Item.Load(reader);
			}

			User = new User();
			User.Id = DataHandler.GetInt32(reader, "UserId");
			User.Email = DataHandler.GetString(reader, "UserEmail");

			if (!String.IsNullOrEmpty(User.Email))
			{
				User.Load(reader);
				User.Address = new Address();
			}

			Address = new Address();
			Address.Id = DataHandler.GetInt32(reader, "AddressId");

			if (Address.Id > 0)
			{
				Address.Load(reader);
			}
		}

		#endregion

		#region Helper methods

		public static ItemStatus GetNextStatus(Item item)
		{
			if (item.Status == ItemStatus.Registered)
			{
				return ItemStatus.Stolen;
			}

			if (item.Status == ItemStatus.Found)
			{
				return ItemStatus.Registered;
			}

			return ItemStatus.Found;
		}

		#endregion
	}
}